﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;

namespace Aker.DevTeam
{
    public static class GenericExtensions
    {
        public static string ToStringNN(this object value)
        {
            string result = "";

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                result = value.ToString();
            }
            return result;
        }

        public static string CamelCasing(this object value)
        {
            string result = "";

            if (!(value == DBNull.Value || value == null || value == ""))
            {
                result = value.ToString();
                List<string> parts = new List<string>();
                foreach (string part in result.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    if (part.Length > 1)
                        parts.Add(part.Substring(0, 1).ToUpper() + part.Substring(1).ToLower());
                    else
                        parts.Add(part.ToUpper());
                }
                result = String.Join(" ", parts);
            }
            return result;
        }

        /// <summary>
        /// Bu fonksyion, aldığı object türünde veriyi integer a çevirir. Çeviremediği durumlarda -1 dönderir.
        /// </summary>
        /// <param name="value">Oject türünde herhangi bir parametre. Null ya da DBNull.Value da olabilir.</param>
        /// <returns>Integer a dönüştürülmüş hali ya da hata durumunda -1</returns>
        public static int ToIntNN(this object value, int exceptioanlValue = -1)
        {
            int result = exceptioanlValue;

            if (value == DBNull.Value)
            {
            }
            else if (value == null || value.ToString() == "")
            {
            }
            else
            {
                try
                {
                    result = Convert.ToInt32(value);
                }
                catch
                {
                }
            }
            return result;
        }


        public static decimal ToDecimalNN(this object value)
        {
            decimal result = 0;

            if (value == DBNull.Value)
            {
            }
            else if (value == null || value.ToString() == "")
            {
            }
            else
            {
                try
                {
                    result = Convert.ToDecimal(value);
                }
                catch
                {
                }
            }
            return result;
        }

        public static int ToComboIndex(this object value)
        {
            return value.ToIntNN(1) - 1;
        }

        public static string ExtractValue(this string source, string caption)
        {
            return source.Substring(caption.Length, source.Length - caption.Length).Trim();
        }

        public static bool StartsWithCI(this string source, string value)
        {
            return source.ToLower().StartsWith(value.ToLower());
        }

        public static bool IsValidEmail(this string email)
        {
            string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
              + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
              + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            return regex.IsMatch(email);
        }

        public static void Bind(this LookUpEdit le, DataTable dataSource, string displayMember, string valueMember)
        {
            le.Properties.DataSource = dataSource;
            le.Properties.DisplayMember = displayMember;
            le.Properties.ValueMember = valueMember;
            le.EditValue = dataSource.Rows[0][valueMember];
        }
    }

    public class Utility
    {
        public static decimal GetEuro(decimal dollar)
        {
            //if (Settings.DollarEuroParity == 1)
            //    return dollar;
            //else
            {
                decimal euro = dollar / Settings.DollarEuroParity;
                decimal result = Math.Ceiling((dollar / Settings.DollarEuroParity * 10)) / 10;
                return result;
            }
        }

        public static RepositoryItemGridLookUpEdit GetGLESablon(DataTable SourceTable, string DisplayMember, string ValueMember)
        {
            RepositoryItemGridLookUpEdit gleSablon = new RepositoryItemGridLookUpEdit();
            gleSablon.AllowNullInput = DefaultBoolean.True;
            gleSablon.View.OptionsView.ColumnAutoWidth = false;
            gleSablon.View.OptionsView.ShowAutoFilterRow = true;
            gleSablon.View.OptionsBehavior.Editable = false;
            gleSablon.View.BestFitMaxRowCount = 50;
            gleSablon.View.OptionsFind.HighlightFindResults = true;
            gleSablon.View.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gleSablon.NullText = string.Empty;
            gleSablon.ImmediatePopup = true;

            gleSablon.PopupFormMinSize = new Size(500, 500);
            gleSablon.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFit;

            gleSablon.Popup += gleSablon_Popup;

            gleSablon.DataSource = SourceTable;
            gleSablon.DisplayMember = DisplayMember;
            gleSablon.ValueMember = ValueMember;

            gleSablon.PopulateViewColumns();

            foreach (GridColumn gc in gleSablon.View.Columns)
            {
                gc.FilterMode = ColumnFilterMode.DisplayText;


            }

            return gleSablon;
        }

        static void gleSablon_Popup(object sender, EventArgs e)
        {
            GridLookUpEdit gle = (GridLookUpEdit)sender;
            gle.Properties.View.BestFitColumns();
            gle.Popup -= gleSablon_Popup;
        }

        public static RepositoryItemGridLookUpEdit GetAutoCompGLE(DataTable dt, string displayMember, string valueMember)
        {
            return Utility.GetGLESablon(dt, displayMember, valueMember);
        }


    }
}